/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wealth.merchant.entity.BusinessDistrict;
import com.wealth.merchant.repository.BusinessDistrictDao;
import com.wealth.merchant.service.IBusinessDistrictService;
import com.wealth.merchant.vo.DistrictItem;
import com.wealth.merchant.vo.DistrictVO;

import iaas.service.impl.BaseServiceImpl;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class BusinessDistrictServiceImpl extends BaseServiceImpl<BusinessDistrict> implements IBusinessDistrictService{

	@Autowired
	private BusinessDistrictDao businessDistrictDao;

	@Override
	public List<BusinessDistrict> getByParentIdIsNullAndStatus(Integer status) throws Exception {
		return businessDistrictDao.findByParentIdIsNullAndStatus(status);
	}
	
	@Override
	public List<BusinessDistrict> getByParentIdAndStatus(
			Long parentId, Integer status) throws Exception {

		return businessDistrictDao.findByParentIdAndStatus(parentId,status);
	}

	
	
	/** ---------------- 微信端 ---------------- */
	/** 惠生活商圈父子列表*/
	public List<DistrictVO> getBusinessDistrictForHuiLife(Integer status)  throws Exception{
		List<DistrictVO> volist = new ArrayList<DistrictVO>();
		List<BusinessDistrict> districtList = businessDistrictDao.findByStatus(status);
		List<BusinessDistrict> pdistrictList = new ArrayList<BusinessDistrict>();
		
		//父节点
		for (BusinessDistrict district : districtList) {
			if(district.getParent()==null){
				pdistrictList.add(district);
			}
		}
		//遍历子节点
		for (BusinessDistrict parent : pdistrictList) {
			DistrictVO vo = new DistrictVO();
			vo.setParent(new DistrictItem(parent.getId(),parent.getName()));
			List<DistrictItem> childList = new ArrayList<DistrictItem>();
			for (BusinessDistrict child : districtList) {
				if(child.getParent()!=null && child.getParent().getId()==parent.getId()){
					childList.add(new DistrictItem(child.getId(), child.getName())); 
				}
			}
			vo.setChildList(childList);
			volist.add(vo);
		}
		
		return volist;
	}




	






}


